;+
; This is designed to behave like the procedure
;	version of create_struct (from ASTROLIB) without
;	having to write a new function. The
;	trade off is that this will run a lot slower
;	because it handles memory poorly (with concatenation)
;SYNTAX
;	rcreate_struct, str, filename, tagnames, descrip
; or
;	rcreate_struct, str,tagnames, descrip
;INPUTS
;	str:
;	filename: does nothing...
;	tagnames:
;	descrip	:
;Notes:
;	see documentaiton for create_struct.pro
;
;Written by R. da Silva, UCSC, 6-7-11
;-

pro rcreate_struct, str, tagnames, descrip, dummy
if n_params() EQ 4 then begin
tagnames=descrip
descrip=dummy
endif

;-*-*-*-*-*
; Parse descrip
if n_elements(descrip) EQ 1 then $
   split=strsplit(descrip, ';', /extract) $
   else split=descrip

;maxnelem=0


dum={x123547_tag1:0}
for i=0, n_elements(tagnames)-1 do begin
sub=strcompress(split[i], /remove_all)
if strmatch(sub, '*(*') EQ 0 then nelem=1 else begin
split2=strsplit(sub, '(', /extract)
split2=strsplit(split2[1], ')', /extract)
split3=strsplit(split2, ',', /extract)
;nsplit=n_elements(split3)
nelem=long64(split3)

endelse
type=strupcase(strmid(sub, 0, 1))

case type of
'D':func='dblarr'
'A':func='strarr'
'B':func='ulonarr'
'L':func='ulonarr'
'I':func='intarr'
'F':func='fltarr'
'E':func='fltarr'
'J':func='lonarr'
endcase


case n_elements(nelem) of
1:begin
	proto=(call_function(func, nelem))
	if nelem EQ 1 then proto=proto[0]
  end
2:proto=call_function(func, nelem[0], nelem[1])
3:proto=call_function(func, nelem[0], nelem[1], nelem[2])
4:proto=call_function(func, nelem[0], nelem[1], nelem[2],nelem[3])
5:proto=call_function(func, nelem[0], nelem[1], nelem[2],nelem[3],nelem[4])
6:begin
   proto=call_function(func, nelem[0], nelem[1], nelem[2],nelem[3],nelem[4],$
			nelem[5])
  end
7:proto=call_function(func, nelem[0], nelem[1], nelem[2],nelem[3],nelem[4],$
			nelem[5],nelem[6])
8:proto=call_function(func, nelem[0], nelem[1], nelem[2],nelem[3],nelem[4],$
			nelem[5],nelem[6],nelem[7])
9:proto=call_function(func, nelem[0], nelem[1], nelem[2],nelem[3],nelem[4],$
			nelem[5],nelem[6],nelem[7],nelem[8])
10:proto=call_function(func, nelem[0], nelem[1], nelem[2],nelem[3],nelem[4],$
			nelem[5],nelem[6],nelem[7],nelem[8],nelem[9])
else:begin
	print, 'Could not make array larger than 10 dimensions'
	return
     end
endcase

;maxnelem=nelem > maxnelem
adder=create_struct(tagnames[i], proto)
if i EQ 0 then str=adder else str=struct_addtags(str, adder)
endfor


;help, str, /st
;print, descrip
;;if n_elements(nelem) NE 1 then begin
;STOP
;endif
end
